package com.itcam.tingshu.user.strategy.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itcam.tingshu.model.user.UserInfo;
import com.itcam.tingshu.model.user.UserPaidAlbum;
import com.itcam.tingshu.model.user.UserVipService;
import com.itcam.tingshu.model.user.VipServiceConfig;
import com.itcam.tingshu.user.mapper.UserInfoMapper;
import com.itcam.tingshu.user.mapper.UserPaidAlbumMapper;
import com.itcam.tingshu.user.mapper.UserVipServiceMapper;
import com.itcam.tingshu.user.mapper.VipServiceConfigMapper;
import com.itcam.tingshu.user.strategy.PaymentStrategy;
import com.itcam.tingshu.vo.user.UserPaidRecordVo;
import org.checkerframework.checker.units.qual.A;
import org.joda.time.LocalDateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author : Cammy.Wu
 * Description : 
 */

@Service("vipPaymentStrategy")
public class VipPaymentStrategy implements PaymentStrategy {

    @Autowired
    private UserVipServiceMapper userVipServiceMapper;

    @Autowired
    private VipServiceConfigMapper vipServiceConfigMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    /**
     * 购买专辑记录
     * @param userPaidRecordVo
     */
    @Override
    public void processPayment(UserPaidRecordVo userPaidRecordVo) {
// 购买会员
        //防止重复消费
        LambdaQueryWrapper<UserVipService> queryWrapper = new LambdaQueryWrapper<UserVipService>().eq(UserVipService::getOrderNo, userPaidRecordVo.getOrderNo());
        Long count = userVipServiceMapper.selectCount(queryWrapper);
        if (count != 0) return;
        // 查询用户购买vip时间
        Long itemId = userPaidRecordVo.getItemIdList().get(0);
        //  根据Id查询vip服务配置对象
        VipServiceConfig vipServiceConfig = vipServiceConfigMapper.selectById(itemId);
        Integer serviceMonth = vipServiceConfig.getServiceMonth();
        // 创建对象
        UserVipService userVipService = new UserVipService();
        /// 赋值
        userVipService.setUserId(userPaidRecordVo.getUserId());
        userVipService.setOrderNo(userPaidRecordVo.getOrderNo());
        userVipService.setStartTime(new Date());
        // 过期时间：
        // 1.第一次购买当前时间+购买月份！  第二次购买可能会发生--续费： 当前过期时间+续费时间！
        // 2.如何判断是否续费：是否vip and vip_expire > 当前系统时间
        UserInfo userInfo = userInfoMapper.selectById(userPaidRecordVo.getUserId());
        // 判断
        if (null != userInfo && userInfo.getIsVip() == 1 && userInfo.getVipExpireTime().after(new Date())) {
            // userInfo.getVipExpireTime() + userInfo.getServiceMonth();
            LocalDateTime localDate = new LocalDateTime(userInfo.getVipExpireTime());
            Date expireTime = localDate.plusMonths(serviceMonth).toDate();
            userVipService.setExpireTime(expireTime);
        } else {
            // 不是续费情况
            userVipService.setExpireTime(new LocalDateTime().plusMonths(serviceMonth).toDate());
        }
        // 保存数据
        userVipServiceMapper.insert(userVipService);
        // 普通用户变为vip    user_info.is_vip = 1;
        userInfo.setIsVip(1);
        userInfo.setVipExpireTime(userVipService.getExpireTime());
        userInfoMapper.updateById(userInfo);
    }
}
